<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户编号" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户编号" />
      </el-form-item>
      <el-form-item label="店员编号" prop="workerClerkId">
        <el-input v-model="formData.workerClerkId" placeholder="请输入店员编号" />
      </el-form-item>
      <el-form-item label="店员用户编号" prop="workerUserId">
        <el-input v-model="formData.workerUserId" placeholder="请输入店员用户编号" />
      </el-form-item>
      <el-form-item label="打赏类型：0-打赏，1-赠礼" prop="rewardType">
        <el-radio-group v-model="formData.rewardType">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="礼物编号" prop="giftId">
        <el-input v-model="formData.giftId" placeholder="请输入礼物编号" />
      </el-form-item>
      <el-form-item label="礼物" prop="gift">
        <el-input v-model="formData.gift" placeholder="请输入礼物" />
      </el-form-item>
      <el-form-item label="礼物数量" prop="count">
        <el-input v-model="formData.count" placeholder="请输入礼物数量" />
      </el-form-item>
      <el-form-item label="礼物总金额" prop="payMoney">
        <el-input v-model="formData.payMoney" placeholder="请输入礼物总金额" />
      </el-form-item>
      <el-form-item label="手续费" prop="fee">
        <el-input v-model="formData.fee" placeholder="请输入手续费" />
      </el-form-item>
      <el-form-item label="实际到账" prop="money">
        <el-input v-model="formData.money" placeholder="请输入实际到账" />
      </el-form-item>
      <el-form-item label="微信" prop="weixin">
        <el-input v-model="formData.weixin" placeholder="请输入微信" />
      </el-form-item>
      <el-form-item label="心动留言" prop="msg">
        <el-input v-model="formData.msg" placeholder="请输入心动留言" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { RewardApi, RewardVO } from '@/api/worker/reward'

/** 打赏记录 表单 */
defineOptions({ name: 'RewardForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  workerClerkId: undefined,
  workerUserId: undefined,
  rewardType: undefined,
  giftId: undefined,
  gift: undefined,
  count: undefined,
  payMoney: undefined,
  fee: undefined,
  money: undefined,
  weixin: undefined,
  msg: undefined,
  status: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RewardApi.getReward(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RewardVO
    if (formType.value === 'create') {
      await RewardApi.createReward(data)
      message.success(t('common.createSuccess'))
    } else {
      await RewardApi.updateReward(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    workerClerkId: undefined,
    workerUserId: undefined,
    rewardType: undefined,
    giftId: undefined,
    gift: undefined,
    count: undefined,
    payMoney: undefined,
    fee: undefined,
    money: undefined,
    weixin: undefined,
    msg: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>